package com.hinotoyk.testsystem.controller;

import com.hinotoyk.testsystem.config.UserToken;
import com.hinotoyk.testsystem.entity.Admin;
import com.hinotoyk.testsystem.entity.Student;
import com.hinotoyk.testsystem.entity.Teacher;
import com.hinotoyk.testsystem.entity.TestPaperInfo;
import com.hinotoyk.testsystem.service.AdminService;
import com.hinotoyk.testsystem.service.StudentService;
import com.hinotoyk.testsystem.service.TeacherService;
import com.hinotoyk.testsystem.service.TestPaperService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: testsystem
 * @description:
 * @author: DYJ
 * @create: 2019-11-19 22:35
 **/
@Controller
public class AdminController {
    @Resource
    private AdminService adminService;

    @Resource
    private StudentService studentService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private TestPaperService testPaperService;

    @RequestMapping("/admin/index")
    public String getIndex(Model model, @ModelAttribute("admin")Admin admin){
        model.addAttribute("admin",admin);
        return "admin/adminIndex";
    }

    @RequestMapping("/admin/login")
    public String getLogin(){
        return "admin/login";
    }


    @RequestMapping("/admin/handleLogin")
    public String handleLogin(Model model, String username, String pw, String loginType,
                        RedirectAttributes redattr, HttpServletRequest request){
        if(username==null || username.equals("")){
            model.addAttribute("msg","管理员用户名不能为空");
            return "admin/login";
        }
        if(pw==null || pw.equals("")){
            model.addAttribute("msg","管理员密码不能为空");
            return "admin/login";
        }

        UserToken token = new UserToken(username,pw,loginType);
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(token);
            if(subject.isAuthenticated()){
                Admin admin = adminService.getAdmin(username);
                redattr.addFlashAttribute("admin",admin);
                return "redirect:/admin/index";
            }
        }catch (Exception e){
            model.addAttribute("msg","用户名或者密码错误");
            return "admin/login";
        }

        return "admin/login";
    }

    @RequestMapping("/admin/manageStudents")
    public String manageStudents(Model model){
        List<Student> students = studentService.getAllStudent();
        model.addAttribute("students",students);
        return "admin/manageStudents";
    }

    @RequestMapping("/admin/manageTeachers")
    public String manageTeachers(Model model){
        List<Teacher> teachers = teacherService.getAllTeacher();
        model.addAttribute("teachers",teachers);
        return "admin/manageTeachers";
    }

    @RequestMapping("/admin/manageTest")
    public String manageTest(Model model){
        List<TestPaperInfo> list = testPaperService.getAllTestPaper();

        List<Teacher> teacherList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            Integer teacherId = list.get(i).getTeacherId();
            Teacher teacher = teacherService.getTeacherById(teacherId);
            teacherList.add(teacher);
        }

        model.addAttribute("testPaperList",list);
        model.addAttribute("teacherList",teacherList);
        return "admin/managesTest";
    }
}
    